package com.hyh.web;

import com.hyh.bean.Role;
import com.hyh.bean.Users;
import com.hyh.dao.UsersDao;
import com.hyh.service.UsersService;
import com.hyh.service.impl.UsersServiceImpl;
import com.hyh.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/user/users")
public class UsersServlet extends HttpServlet {

    private UsersService service = new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("roleList".equals(method)){
            roleList(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else {
            select(req,resp);
        }
    }

    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.接收参数（分页 + 模糊查询）
        String index = req.getParameter("index");
        int pageIndex = (index == null || index.length() == 0)? 1:Integer.parseInt(index);
        //2.调取service方法（1.查询数据列表的方法 2.查询总条数的方法）
        PageUtil pageUtil = new PageUtil();
        List<Users> usersList = service.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = service.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDateList(usersList);
        //3.存值跳转页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    // 新增人员（角色列表）
    protected void roleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> roleList = service.getRoleList();
        //存值跳转页面
        req.setAttribute("rlist",roleList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    // 新增人员
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        Users users = new Users();
        users.setLoginName(loginName);
        users.setPassword(password);
        users.setRealName(realName);
        users.setSex(Integer.parseInt(sex));
        users.setRoleId(Integer.parseInt(roleId));
        users.setEmail(email);
        users.setPhone(phone);
        users.setAddress(address);
        users.setCardId(cardId);
        users.setDesc(desc);
        int i = service.insertRole(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0){
            writer.println("<script>alert('新增成功');location.href='/power/user/users';</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/power/user/users?method=roleList';</script>");
        }

    }
    // 新增人员（角色列表）
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        int delete = service.delete(Integer.parseInt(uid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (delete > 0){
            writer.println("<script>alert('删除成功');location.href='/power/role/roles';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power/role/roles';</script>");
        }
    }
}
